import 'package:part_time_job_pro/common/exception/exp_handler.dart';
import 'package:part_time_job_pro/common/models/part_job_type.dart';
import 'package:part_time_job_pro/common/models/part_time_job.dart';
import 'package:part_time_job_pro/common/models/part_type_temp.dart';
import 'package:part_time_job_pro/common/models/sys_dict_data.dart';
import 'package:part_time_job_pro/common/utils/def_type.dart';
import 'package:part_time_job_pro/common/utils/request_util.dart';
import 'package:part_time_job_pro/common/values/respones_code.dart';
import 'package:part_time_job_pro/common/values/server_config.dart';

class JobApi {
  late RequestUtil apiUtil;
  //创建实例
  JobApi() {
    apiUtil = RequestUtil.getInstance();
  }

/*
   * 搜索兼职类型
   */
  searchPartJobType(RequestCallBack requestCallBack) async {
    try {
      List<PartJobType> typeList;
      Map<String, dynamic> params = {};
      var result = await apiUtil.post(HttpConfig.searchPartJobType,
          decode: true, data: params);
      int resultCode = result[ResponesCode.codeTag];
      if (resultCode == ResponesCode.successCode) {
        var resultList = result[ResponesCode.dataTag];
        typeList = resultList
            .map<PartJobType>((item) => PartJobType.fromJson(item))
            .toList();
        requestCallBack(typeList);
      } else {
        requestCallBack(result);
      }
    } catch (e) {
      requestCallBack(ExceptionHandler.handleDataException(e));
    }
  }

  /*
   * 获取兼职类型
   */
  getPartJobType(RequestCallBack requestCallBack) async {
    try {
      List<PartJobType> typeList;
      Map<String, dynamic> params = {};
      var result = await apiUtil.post(HttpConfig.partJobType,
          decode: true, data: params);
      int resultCode = result[ResponesCode.codeTag];
      if (resultCode == ResponesCode.successCode) {
        var resultList = result[ResponesCode.dataTag];
        typeList = resultList
            .map<PartJobType>((item) => PartJobType.fromJson(item))
            .toList();
        requestCallBack(typeList);
      } else {
        requestCallBack(result);
      }
    } catch (e) {
      requestCallBack(ExceptionHandler.handleDataException(e));
    }
  }

  /*
   * 获取全职类型
   */
  getFullJobType(RequestCallBack requestCallBack) async {
    try {
      List<PartJobType> typeList;
      Map<String, dynamic> params = {};
      var result = await apiUtil.post(HttpConfig.fullJobType,
          decode: true, data: params);
      int resultCode = result[ResponesCode.codeTag];
      if (resultCode == ResponesCode.successCode) {
        var resultList = result[ResponesCode.dataTag];
        typeList = resultList
            .map<PartJobType>((item) => PartJobType.fromJson(item))
            .toList();
        requestCallBack(typeList);
      } else {
        requestCallBack(result);
      }
    } catch (e) {
      requestCallBack(ExceptionHandler.handleDataException(e));
    }
  }

  /*
   * 获取兼职类型模板
   */
  getPartJobTypeTemp(RequestCallBack requestCallBack, int typeId) async {
    try {
      List<PartTypeTemp> typeList;
      Map<String, dynamic> params = {"typeId": typeId};
      var result = await apiUtil.post(HttpConfig.getPartJobTypeTemp,
          decode: true, data: params);
      int resultCode = result[ResponesCode.codeTag];
      if (resultCode == ResponesCode.successCode) {
        var resultList = result[ResponesCode.dataTag];
        typeList = resultList
            .map<PartTypeTemp>((item) => PartTypeTemp.fromJson(item))
            .toList();
        requestCallBack(typeList);
      } else {
        requestCallBack(result);
      }
    } catch (e) {
      requestCallBack(ExceptionHandler.handleDataException(e));
    }
  }

  /*
   * 获取兼职类型模板
   */
  submitPartTimeJob(
      RequestCallBack requestCallBack, Map<String, dynamic> params) async {
    try {
      var result = await apiUtil.post(HttpConfig.savePartTimeJob,
          decode: true, data: params);
      requestCallBack(result);
    } catch (e) {
      requestCallBack(ExceptionHandler.handleDataException(e));
    }
  }

  /*
   * 获取字典数据
   */
  getPartTimeJobList(RequestCallBack requestCallBack, String status,
      int pageNum, int pageSize) async {
    try {
      List<PartTimeJob> partJobList;
      Map<String, dynamic> params = {
        "status": status,
        "pageNum": pageNum,
        "pageSize": pageSize
      };
      var result = await apiUtil.post(HttpConfig.partTimeJobList,
          decode: true, data: params);
      int resultCode = result[ResponesCode.codeTag];
      if (resultCode == ResponesCode.successCode) {
        var resultList = result[ResponesCode.dataTag];
        partJobList = resultList
            .map<PartTimeJob>((item) => PartTimeJob.fromJson(item))
            .toList();
        requestCallBack(partJobList);
      } else {
        requestCallBack(result);
      }
    } catch (e) {
      requestCallBack(ExceptionHandler.handleDataException(e));
    }
  }

  /*
   * 获取字典数据
   */
  getDictDataList(RequestCallBack requestCallBack, String dataKey) async {
    try {
      List<SysDictData> typeList;
      Map<String, dynamic> params = {"dictKey": dataKey};
      var result = await apiUtil.post(HttpConfig.getDictData,
          decode: true, data: params);
      int resultCode = result[ResponesCode.codeTag];
      if (resultCode == ResponesCode.successCode) {
        var resultList = result[ResponesCode.dataTag];
        typeList = resultList
            .map<SysDictData>((item) => SysDictData.fromJson(item))
            .toList();
        requestCallBack(typeList);
      } else {
        requestCallBack(result);
      }
    } catch (e) {
      requestCallBack(ExceptionHandler.handleDataException(e));
    }
  }
}
